// ==================================================================
// 描 述：
// 作 者：王征:D
// 创建时间：2019/05/21 11:59:40
// ==============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
using Yunchang;
using Table.DataModel;

namespace AutoChess
{
    public class ACChessInfoPanel : MonoBehaviour
    {
        public TextureKit heroIcon;
        public GroupSpriteKit[] GroupSprites;
        public GroupSpriteKit RareSprite;
        public GameObject[] heroStars;
        public Text heroName;
        public Text heroType;
        public Text heroTargetType;
        public Text costText;
        // groupIcons
        public Text[] topPropValues;

        public Transform skillRoot;
        public SkillIntroductionItem skill1;
        //public SkillIntroductionItem skill2;
        //public SkillIntroductionItem skill3;
        //public SkillIntroductionItem skill4;

        public List<Text> propValues;

        public GameObject upArrow;
        public GameObject downArrow;
        public ScrollRect scroll;

        public ACGroupBuffDetailPanel[] detailPanels;

        private RectTransform contentRect;
        private RectTransform viewPortRect;
        private int chessId;
        private void Awake()
        {
            scroll.onValueChanged.AddListener(OnScrollValueChanged);
            contentRect = scroll.content;
            viewPortRect = contentRect.parent as RectTransform;
        }

        public void SetData(int chessId)
        {
            this.chessId = chessId;
            var tableChess = Table.Blo.AutoChessBlo.GetChessTable(chessId);
            if (tableChess == null)
                return;
            var tableChessHero = Table.Blo.AutoChessBlo.GetChessHero(tableChess.heroId);
            if (tableChessHero == null)
                return;
            var tableHero = Table.Blo.TableHeroBlo.GetTableHero(tableChess.heroId);
            var heroData = StaticData.playerData.role.GetHeroData(tableChess.heroId);
            var tableRare = Table.Blo.AutoChessBlo.GetTableRareByRare(tableChessHero.rare);
            if (heroData == null || tableHero == null)
            {
                ExLog.LogError("找不到这个角色信息 hero ID:" + tableChess.heroId);
                return;
            }

            heroName.text = TableTextBlo.TransferText(tableHero.heroName);
            if (costText != null)
                costText.text = "x " + tableChess.buyPrice.ToString();
            if (heroIcon != null)
                heroIcon.TextureName = Table.Blo.TableHeroBlo.GetHeroIcon(tableChess.heroId, 0);
            heroTargetType.text = TableTextBlo.TransferText(tableHero.heroAttackTarget);
            if (heroType != null)
                heroType.text = TableTextBlo.TransferText(tableChessHero.heroStyle);
            int i = 0;
            for (; i < heroStars.Length; i++)
            {
                heroStars[i].SetActive(i < tableChess.star);
            }
            if (RareSprite != null)
                RareSprite.SetSpriteName(tableRare.rareFlagIcon);

            float rectHeight = 0f;
            float skillRectHeight = (skill1.transform as RectTransform).sizeDelta.y;


            //skill1.gameObject.SetActive(tableChess.skill1 != 0);
            //skill2.gameObject.SetActive(tableChess.skill2 != 0);
            //skill3.gameObject.SetActive(tableChess.skill3 != 0);
            //skill4.gameObject.SetActive(tableChess.ultimateSkill != 0);
            skillRoot.DestroyChildren();
            if (tableChess.skill1 != 0)
            {
                var skillTmp = Instantiate(skill1, skillRoot);
                skillTmp.InitSkillDes(tableChess.skill1);
                skillTmp.SetGameObjectActiveEx(true);
                rectHeight += skillRectHeight;
            }
            if (tableChess.skill2 != 0)
            {
                var skillTmp = Instantiate(skill1, skillRoot);
                skillTmp.InitSkillDes(tableChess.skill2);
                skillTmp.SetGameObjectActiveEx(true);
                rectHeight += skillRectHeight;
            }
            if (tableChess.skill3 != 0)
            {
                var skillTmp = Instantiate(skill1, skillRoot);
                skillTmp.InitSkillDes(tableChess.skill3);
                skillTmp.SetGameObjectActiveEx(true);
                rectHeight += skillRectHeight;
            }
            if (tableChess.ultimateSkill != 0)
            {
                var skillTmp = Instantiate(skill1, skillRoot);
                skillTmp.InitSkillDes(tableChess.ultimateSkill);
                skillTmp.SetGameObjectActiveEx(true);
                rectHeight += skillRectHeight;
            }
            if (!string.IsNullOrEmpty(tableChess.passiveSkills))
            {
                var skills = tableChess.passiveSkills.Split("&&".ToCharArray());
                if (skills != null)
                {
                    for (int j = 0; j < skills.Length; j++)
                    {
                        if (!string.IsNullOrEmpty(skills[j]))
                        {
                            var skillData = skills[j].Split(',');

                            if (skillData.Length == 2)
                            {
                                int skillId;
                                int.TryParse(skillData[0], out skillId);
                                var tableSkill = Table.Blo.TableHeroSkillBlo.GetTableSkill(skillId);
                                if(tableSkill != null && !string.IsNullOrEmpty(tableSkill.skillDesc))
                                {
                                    var skillTmp = Instantiate(skill1, skillRoot);
                                    skillTmp.InitSkillDes(skillId);
                                    skillTmp.SetGameObjectActiveEx(true);
                                    rectHeight += skillRectHeight;
                                }
                            }
                        }
                    }
                }
            }

            var propRectTransform = (skillRoot as RectTransform);
            var propRectSize = propRectTransform.sizeDelta;
            propRectTransform.sizeDelta = new Vector2(propRectSize.x, rectHeight);

            propValues[0].text = ((int)tableChess.hp).ToString();
            propValues[1].text = ((int)tableChess.atk).ToString();
            propValues[2].text = ((int)tableChess.def).ToString();
            propValues[3].text = ((int)tableChess.speed).ToString();
            topPropValues[0].text = ((int)tableChess.hp).ToString();
            topPropValues[1].text = ((int)tableChess.atk).ToString();
            topPropValues[2].text = ((int)tableChess.def).ToString();
            topPropValues[3].text = ((int)tableChess.speed).ToString();
            propValues[4].text = ((tableChess.cri) * 100).ToString("F1") + "%";
            propValues[5].text = ((float)tableChess.anticri * 100).ToString("F1") + "%";
            propValues[6].text = ((float)tableChess.antiblock * 100).ToString("F1") + "%";
            propValues[7].text = ((float)tableChess.block * 100).ToString("F1") + "%";
            propValues[8].text = ((float)tableChess.hit * 100).ToString("F1") + "%";
            propValues[9].text = ((float)tableChess.dodge * 100).ToString("F1") + "%";
            propValues[10].text = ((float)tableChess.cridamage * 100).ToString("F1") + "%";
            propValues[11].text = ((float)tableChess.antiwhole * 100).ToString("F1") + "%";

            var groups = tableChessHero.acHeroGroups.Split(';');
            i = 0;
            for (; i < groups.Length; i++)
            {
                var group = int.Parse(groups[i]);
                var tableGroup = Table.Blo.AutoChessBlo.GetGroup(group);
                if (tableGroup != null)
                {
                    GroupSprites[i].gameObject.SetActive(true);
                    GroupSprites[i].SetSpriteName(tableGroup.altasName);
                }
            }
            for (; i < GroupSprites.Length; i++)
            {
                GroupSprites[i].gameObject.SetActive(false);
            }

            gameObject.SetActive(true);

            OnScrollValueChanged(Vector2.zero);
        }

        public void Close()
        {
            gameObject.SetActive(false);
            for (int i = 0; i < detailPanels.Length; i++)
            {
                detailPanels[i].gameObject.SetActive(false);
            }
        }

        private void OnScrollValueChanged(Vector2 vec)
        {
            downArrow.SetActive(viewPortRect.rect.height < (contentRect.rect.height - contentRect.anchoredPosition.y));
            upArrow.SetActive(contentRect.anchoredPosition.y > 0.1f);
            for (int i = 0; i < detailPanels.Length; i++)
            {
                detailPanels[i].gameObject.SetActive(false);
            }
        }

        public void OnSelectGroup(int index)
        {
            var tableChess = Table.Blo.AutoChessBlo.GetChessTable(chessId);
            var tableChessHero = Table.Blo.AutoChessBlo.GetChessHero(tableChess.heroId);
            for (int i = 0; i < 4; i++)
            {
                detailPanels[i].gameObject.SetActive(i == index);
            }
            detailPanels[index].SetData(int.Parse(tableChessHero.acHeroGroups.Split(';')[index]), 100);
        }

        [ContextMenu("Reset Ref")]
        public void UIRef()
        {
            Transform Groups = transform.Find("Scroll View/Viewport/Content/TopContent/propertyPoint/PropBg");
            for (int i = 0; i < Groups.childCount; i++)
            {
                Transform cell = Groups.GetChild(i);
                Text num = cell.Find("Num").GetComponent<Text>();
                propValues.Add(num);
            }
        }
    }
}
